SUBROUTINE WRITE_GC(CHID,TBEG,TEND)

IMPLICIT NONE

INTEGER, PARAMETER :: FB = SELECTED_REAL_KIND(6)
INTEGER :: I,J

CHARACTER(256) CHID,QFILE
INTEGER :: RCODE
INTEGER :: BATCHMODE

REAL TEND,TBEG
REAL, ALLOCATABLE, DIMENSION(:,:) :: VERTS,TFACES,DISP,NEWVERTS
INTEGER, ALLOCATABLE, DIMENSION(:) :: SURF_IDS,MATL_IDS,NODE
INTEGER, ALLOCATABLE, DIMENSION(:,:) :: FACES,VOLUS

REAL(FB) TIME
INTEGER ONE_INTEGER,VERSION,N_FLOATS,N_INTS,FIRST_FRAME_STATIC,N_FACES,N_VERTS,N_VOLUS
INTEGER SEL
INTEGER :: ONE=1

REAL(FB) :: DUMMY
INTEGER :: IDUMMY


!WRITE(6,*) ' Enter Job ID string (CHID):'
!READ(*,'(a)') CHID

! Set a few default values
BATCHMODE=0

!VARIABLE_NUMBER: DO VAR=1,VARIABLE
!VAR=1
500 CONTINUE

QFILE = TRIM(CHID)//'_01.ge'
OPEN(13,FILE=QFILE,FORM='UNFORMATTED',STATUS='OLD', IOSTAT=RCODE)
IF (RCODE.NE.0) THEN
  CLOSE(13)
!CYCLE
ENDIF

! read header

READ(13) ONE_INTEGER
READ(13) VERSION
READ(13) N_FLOATS, N_INTS, FIRST_FRAME_STATIC

! read and ignore static frame if present

READ(13) TIME
READ(13) N_VERTS, N_FACES, N_VOLUS
IF (N_VERTS>0) READ(13) (DUMMY,J=1,3*N_VERTS)
IF (N_FACES>0) THEN
   READ(13) (IDUMMY,J=1,3*N_FACES)
   READ(13) (IDUMMY,J=1,N_FACES)
   READ(13) (DUMMY,J=1,6*N_FACES)
ENDIF
IF (N_VOLUS>0) THEN
   READ(13) (IDUMMY,J=1,4*N_VOLUS)
   READ(13) (IDUMMY,J=1,N_VOLUS)
ENDIF

! read and save first dynamic frame

READ(13) TIME
!WRITE(6,*) TIME 
READ(13) N_VERTS, N_FACES, N_VOLUS 
!WRITE(6,*) N_VERTS, N_FACES, N_VOLUS

!OPEN(16,FILE='verts.dat',FORM='FORMATTED',STATUS='UNKNOWN')

IF (N_VERTS>0) THEN
  ALLOCATE (VERTS(3,N_VERTS))            
  ALLOCATE (NEWVERTS(3,N_VERTS))     
  READ(13) VERTS
!  WRITE(6,*) VERTS
!  DO I=1,N_VERTS
!    WRITE(16,'(F8.5,F8.5,F8.5)') VERTS(1,I),VERTS(2,I),VERTS(3,I)
!  ENDDO
ENDIF

IF (N_FACES>0) THEN
   ALLOCATE (FACES(N_FACES,3))
   ALLOCATE (SURF_IDS(N_FACES))
   ALLOCATE (TFACES(N_FACES,6))
   READ(13) FACES
!   WRITE(6,*) FACES
   READ(13) SURF_IDS
!   WRITE(6,*) SURF_IDS
   READ(13) TFACES
ENDIF
IF (N_VOLUS>0) THEN
   ALLOCATE (VOLUS(N_VOLUS,4))
   ALLOCATE (MATL_IDS(N_VOLUS))
   READ(13) VOLUS
!   WRITE(6,*) VOLUS
   READ(13) MATL_IDS
!   WRITE(6,*) MATL_IDS
ENDIF

CLOSE(13) 

OPEN(13,FILE=TRIM(CHID)//'.gc',FORM='UNFORMATTED',STATUS='REPLACE')
WRITE(13) ONE
!WRITE(6,*) ONE
WRITE(13) VERSION
!WRITE(6,*) VERSION

ALLOCATE (DISP(3,N_VERTS))  
ALLOCATE (NODE(N_VERTS))
OPEN(15,FILE=TRIM(CHID)//'_ansys_displacements.dat',FORM='FORMATTED',STATUS='OLD')
!READ(15,'(I8)') N_STEPS
WRITE(6,*)"COUPLING: Reading displacements file"

!DO J=1,N_STEPS 
READ(15,'(F8.3)') TIME
IF (TIME==TEND) SEL=1
IF (TIME==TBEG) SEL=2
IF (TIME>TBEG .AND. TIME<TEND) SEL=3
SELECT CASE (SEL)
  CASE (1) ! TIME = TEND
    WRITE(6,'(A,F5.1,A)') " COUPLING: Time = ", TIME,'s' 
    DO I=1,N_VERTS
      READ(15,'(I8)', ADVANCE='NO') NODE(I)
      !WRITE(6,*) NODE(I)
      READ(15,'(F8.5,F8.5,F8.5)') DISP(1,I),DISP(2,I),DISP(3,I)
      !WRITE(6,*) DISP(1,I),DISP(2,I),DISP(3,I)
    ENDDO
    CLOSE (15)  

    DO I=1,N_VERTS
      NEWVERTS(1,I)=VERTS(1,I)+DISP(1,I)
      NEWVERTS(2,I)=VERTS(2,I)+DISP(2,I)
      NEWVERTS(3,I)=VERTS(3,I)+DISP(3,I)
    ENDDO   
    !WRITE(6,*) NEWVERTS

    WRITE(6,*)"COUPLING: Writing ",TRIM(CHID),".gc file"

    WRITE(13) TIME
    WRITE(13) N_VERTS, N_FACES, N_VOLUS 
    IF (N_VERTS>0) WRITE(13) NEWVERTS
    IF (N_FACES>0) THEN
      WRITE(13) FACES
      WRITE(13) SURF_IDS
      WRITE(13) TFACES
    ENDIF
    IF (N_VOLUS>0) THEN
      WRITE(13) VOLUS
      WRITE(13) MATL_IDS
    ENDIF

    CLOSE (13)

  CASE (2) ! TIME = TBEG
    OPEN(20,FILE=TRIM(CHID)//'.stop',FORM='FORMATTED',STATUS='NEW')
    WRITE (6,*) 'ERROR: ANSYS failed in run the simulation until the defined time step!'
    WRITE (6,*) '       Check structural simulation for failure or stability issues.'
    STOP

  CASE (3) ! TBEG < TIME < TEND    
    OPEN(20,FILE=TRIM(CHID)//'.stop',FORM='FORMATTED',STATUS='NEW')
    WRITE (6,*) 'ERROR: ANSYS failed in run the simulation until the defined time step!'
    WRITE (6,*) '       Check structural simulation for failure or stability issues.'
    WRITE(6,'(A,F5.1)') "     Time = ", TIME
    DO I=1,N_VERTS
      READ(15,'(I8)', ADVANCE='NO') NODE(I)
      !WRITE(6,*) NODE(I)
      READ(15,'(F8.5,F8.5,F8.5)') DISP(1,I),DISP(2,I),DISP(3,I)
      !WRITE(6,*) DISP(1,I),DISP(2,I),DISP(3,I)
    ENDDO
    CLOSE (15)  

    DO I=1,N_VERTS
      NEWVERTS(1,I)=VERTS(1,I)+DISP(1,I)
      NEWVERTS(2,I)=VERTS(2,I)+DISP(2,I)
      NEWVERTS(3,I)=VERTS(3,I)+DISP(3,I)
    ENDDO   
    !WRITE(6,*) NEWVERTS

    WRITE(6,*)"COUPLING: Writing ",TRIM(CHID),".gc file"

    WRITE(13) TIME
    WRITE(13) N_VERTS, N_FACES, N_VOLUS 
    WRITE(6,*) N_VERTS, N_FACES, N_VOLUS 
    IF (N_VERTS>0) WRITE(13) NEWVERTS
    IF (N_FACES>0) THEN
      WRITE(13) FACES
      WRITE(13) SURF_IDS
      WRITE(13) TFACES
    ENDIF
    IF (N_VOLUS>0) THEN
      WRITE(13) VOLUS
      WRITE(13) MATL_IDS
    ENDIF

    CLOSE (13)
    STOP

END SELECT    

!ENDDO

!LOOP_FDS2AST: DO I=1,COUNTER
!    WRITE (OUTFILE,'(g8.0)') INT(NOMASTER(I,1))
!    PRINT *, OUTFILE
!****************

!OPEN(100+VAR,FILE=TRIM(OUTFILE)//'.dat',FORM='FORMATTED',STATUS='UNKNOWN')
!      DO J=1,SIZE+1
!        WRITE (100+VAR,'(E12.5)', ADVANCE='NO') V_TIME(J)
!        WRITE (100+VAR,'(E12.5)', ADVANCE='YES') M_AST(I,J)
!      ENDDO
!    CLOSE (100+VAR)
!100 CONTINUE

!ENDDO LOOP_FDS2AST

!ENDDO IN_LOOP

!ENDDO VARIABLE_NUMBER

END SUBROUTINE WRITE_GC